package com.zf.common.json.config;

import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * Json的序列化配置：
 * 设置全局的LocalDateTime、LocalDate、LocalTime日期序列化与反序列化,
 * 此种方式兼容在 配置文件中设置全局格式化java.util.Date类型，兼容@JsonFormat注解。
 * 设置 Long/long 转为json时 使用字符串类型
 *
 * @author Zhou Feng
 */
@Configuration
public class JsonSerializerConfig {

    /**
     * JacksonAutoConfiguration类中：
     * Jackson的 ObjectMapper 对象的自动创建过程：
     * 1、Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
     * 2、builder 对象会调用所有被声明的 Jackson2ObjectMapperBuilderCustomizer对象的customize()方法。
     * 3、ObjectMapper objectMapper = builder.createXmlMapper(false).build();
     * 4、使用 objectMapper 对象来完成："json <-> 对象" 互相转换
     */
    @Order(Ordered.LOWEST_PRECEDENCE)
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        // return new Jackson2ObjectMapperBuilderCustomizer() {
        //     @Override
        //     public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
        //
        //     }
        // };
        return builder -> {
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");

            // 返回时间序列化
            builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(dateTimeFormatter));
            builder.serializerByType(LocalDate.class, new LocalDateSerializer(dateFormatter));
            builder.serializerByType(LocalTime.class, new LocalTimeSerializer(timeFormatter));

            // 接收时间反序列化
            builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(dateTimeFormatter));
            builder.deserializerByType(LocalDate.class, new LocalDateDeserializer(dateFormatter));
            builder.deserializerByType(LocalTime.class, new LocalTimeDeserializer(timeFormatter));

            // 解决后台Long值传递给前端精度丢失问题 -- 方式1
            builder.serializerByType(Long.class, ToStringSerializer.instance);
            builder.serializerByType(Long.TYPE, ToStringSerializer.instance);

            // // 给指定类设置自定义序列化器
            // builder.serializerByType(PageInfo.class, MyPageInfoSerializer.instance);

            // // 解决后台Long值传递给前端精度丢失问题 -- 方式2
            // // 如果定义了多个 Jackson2ObjectMapperBuilderCustomizer，
            // // 那么只有最后一个被执行的 Jackson2ObjectMapperBuilderCustomizer 的内的 #builder.modules()方法才会有效，前面的都会被覆盖。
            // SimpleModule module = new SimpleModule();
            // module.addSerializer(Long.class, ToStringSerializer.instance);
            // module.addSerializer(Long.TYPE, ToStringSerializer.instance);
            // builder.modules(module);
        };
    }


    /**
     * 手动创建 Jackson的 ObjectMapper 对象
     */
    // @Bean
    // public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
    //     //解决后台Long值传递给前端精度丢失问题
    //     ObjectMapper objectMapper = builder.createXmlMapper(false).build();
    //     objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
    //     SimpleModule module = new SimpleModule();
    //     module.addSerializer(Long.class, ToStringSerializer.instance);
    //     module.addSerializer(Long.TYPE, ToStringSerializer.instance);
    //     // 给指定类设置自定义序列化器
    //     module.addSerializer(PageInfo.class, MyPageInfoSerializer.instance);

    //     objectMapper.registerModule(module);
    //     return objectMapper;
    // }

}

